/**
 * Created by Myron on 2015/9/10.
 */
var async = require('async');
var config = require('../config.js');
var signDayM = require('../models/signday.js');

module.exports = {
    //点击购物车按钮进入购物车
    index: function (req, res) {
        var uid = req.AV.user.id;
        console.log(uid);
        res.render('activity/signday', {title: '每日签到'});
    },
    //进入页面获取数据
    getSignDayValueAjax:function(req, res){
        var uid = req.AV.user.id;
        /***模拟数据***/
    /*\
     var MonthSignDay=111; //每月连续签到天数
     var AllSignDay=112; //总共连续签到天数
     var SignState=false; //当天是否签到状态
     var GiftArray;
     GiftArray=[
            {
                D3ObjectId: "111",
                D3Value: 0
            },
            {
                D15ObjectId: "222",
                D15Value: 0
            },
            {
                D30ObjectId: "",
                D30Value: 0
            },
            {
                D90ObjectId: "",
                D90Value: 0
            },
            {
                D180ObjectId: "",
                D180Value: 0
            },
            {
                D365ObjectId: "",
                D365Value:0
            }
        ];*/
        /***模拟数据***/
        //var promise = new AV.Promise();
        async.waterfall([
            function(callback) {
                //获取GiftArray
                var date=new Date();
                signDayM.getSingDayGiftArray(uid, date.getFullYear(),date.getMonth() + 1).then(function(result){

                    callback(null,result.GiftArray);
                });
            },
            function(GiftArray, callback) {
                signDayM.getSignDayValue(uid).then(function(result){
                    callback(null,{code:result.code,MonthSignDay:result.MonthSignDay,AllSignDay:result.AllSignDay,SignState:result.SignState,GiftArray:GiftArray });
                }).catch(function(result){
                    callback(null,{code:result.code,MonthSignDay:result.MonthSignDay,AllSignDay:result.AllSignDay,SignState:result.SignState,GiftArray:GiftArray });
                });
            }
        ], function (err, result) {
            res.json(result);
        });


    },
    //获取签到历史数据
    getSignDayDetailAjax:function (req, res) {
        var month = req.query.Month;
        var uid = req.AV.user.id;
        /***模拟数据***/
        signDayM.getSignDayDetail(uid,month).then(function(result){
            res.json(result);
        });
        //var jsonresult = {code: 0, data:[1,3,4,5,6,7,25]};
        //res.json(jsonresult);
    },
    //签到操作接口
    setSignDayActionAjax:function (req, res) {
        var uid = req.AV.user.id;
        signDayM.setSignDayAction(uid).then(function(result){
            res.json(result);
        }).catch(function(result){
            res.json(result);
        })
    },
    //领取签到商品
    setSignDayGiftGiveAjax:function (req, res) {
        var GiftGiveID=req.query.GiftGiveID;
        var RuleDays=parseInt(req.query.RuleDays);
        var uid = req.AV.user.id;
        switch(RuleDays)
        {
            case 3:
                signDayM.setSignDayGiftGiveJiFeng(uid,GiftGiveID).then(function(result){
                    res.json(result);
                }).catch(function(err){
                    res.json(err);
                })
                break;
            case 15:
                 signDayM.setSignDayGiftGiveYouHuiJuan(uid,GiftGiveID,'签到优惠券现金10圆批次').then(function(result){
                     res.json(result);
                 }).catch(function(err){
                     res.json(err);
                 })
                break;
            case 30:
                signDayM.setSignDayGiftCart(uid,GiftGiveID).then(function(result){
                    res.json(result);
                }).catch(function(err){
                    res.json(err);
                })
                break;
            case 90:
                signDayM.setSignDayGiftCart(uid,GiftGiveID).then(function(result){
                    res.json(result);
                }).catch(function(err){
                    res.json(err);
                })
                break;
            case 180:
                signDayM.setSignDayGiftGiveYuE(uid,GiftGiveID).then(function(result){
                    res.json(result);
                }).catch(function(err){
                    res.json(err);
                })
                break;
            case 365:
                signDayM.setSignDayGiftGiveYuE(uid,GiftGiveID).then(function(result){
                    res.json(result);
                }).catch(function(err){
                    res.json(err);
                })
                break;
            default:
                //都不执行时的代码块
                var jsonresult = {code: 0,msg:"领取失败！"};
                res.json(jsonresult);
        }
    },

}